import { _decorator, Component, Node, ProgressBar, Label, Collider2D, UITransform } from 'cc';
import { Battle } from '../../Battle';
import { EnEvent } from '../../Common/const/EnEvent';
import { GameAPI } from '../../Common/GameAPI';
import { Enum_ColliderGroup } from '../../Enum';
import { RedBlinkEffect } from '../RedBlinkEffect';
const { ccclass, property } = _decorator;

@ccclass('MainTowerController')
export class MainTowerController extends Component {

    @property(Node)
    private levelNum: Node = null;
    @property(Node)
    public hpProgress: Node = null;

    private hpNode: UITransform;
    private hpMaxWidth: number = 0

    private levelEnergyValues: Array<number> = [];
    private curLevel: number = 0;

    private levelLabelNum: Label;
    private maxHp: number = 10000;
    private curHp: number = 0;
    private redBlinkEffect: RedBlinkEffect;

    start() {
        this.hpNode = this.hpProgress.getComponent(UITransform);
        this.hpMaxWidth = this.hpNode.width;
        this.redBlinkEffect = this.node.getComponent(RedBlinkEffect);
        Battle.MainTowerNode = this.node;
        this.levelEnergyValues.push(5);
        this.levelEnergyValues.push(10);
        this.levelEnergyValues.push(15);
        this.levelLabelNum = this.levelNum.getComponent(Label);
        this.curHp = this.maxHp;
        this.UpgradeTower();
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    UpdateMainTowerHpProgress(value) {
        this.curHp = this.curHp - value;
        if (this.curHp <= 0) {
            this.curHp = 0;
            //结束
            GameAPI.FireGEvent(EnEvent.GameFailEvent);
            return;
        }
        else {

        }
        this.UpdateProgressBar();
    }

    UpgradeTower() {
        // this.curLevel++;
        // this.levelLabelNum.string = 0 + "/" + this.maxHp;
        // if (this.hpProgress) {

        // }
    }

    UpdateProgressBar() {
        this.levelLabelNum.string = this.curHp + "/" + this.maxHp;
        let width = this.hpMaxWidth * (this.curHp / this.maxHp);
        this.hpNode.setContentSize(width, this.hpNode.height);
        if (this.redBlinkEffect) {
            this.redBlinkEffect.startBlink();
        }
    }

}


